/**
 * 游戏商城显示类
 */
class gameShopView extends eui.Component {
	public constructor(game: any) {
		super();
		this.gameHandle = game;
	}

	/**全部商品 */
	public allShopData: goods[] = [];

	/**显示的商品 */
	public allShopShow: any;

	private gameHandle;

	public init() {
		this.allShopShow = this.gameHandle.shops;

		this.gameHandle.allShopCallBack();
	}

	/**显示商品
	 * shopType 商品类型 装饰/拖尾
	 */
	public displayGoods(shopType: number) {
		var allGoods = this.allShopData;
		var shops = [];
		shops = this.allShopData.filter(e => {
			if (e.goodsType == shopType)
				return e;
		});

		for (var t = 0; t < shops.length; t++) {
			if (shops[t].goodsType == shopType) {
				var shopPoint = this.getPositionByIndex(t);
				shops[t].DisplayShops(shopPoint.x, shopPoint.y);
				this.allShopShow.addChild(shops[t]);
			}
		}
		this.displayHaveGoods();
	}


	/**获取商品位置 */
	private getPositionByIndex(t) {
		var startPoint = { x: 120, y: 100 };
		return {
			"x": (t % 3 == 3 || t % 3 == 0) ? startPoint.x : startPoint.x + (t % 3) * 210,
			"y": t / 3 == 0 ? startPoint.y : startPoint.y + Math.floor(t / 3) * 210
		}
	}

	public userBag = [];

	/**玩家的背包数据处理 */
	public ownDataDispos(ownData: any) {
		this.userBag = ownData.shopData;
	}

	/**显示已有商品 */
	public displayHaveGoods() {
		if (this.userBag == null)
			return;
		var playerOwnData = this.userBag;
		for (var t = 0; t < playerOwnData.length; t++) {
			this.allShopData.forEach(e => {
				if (playerOwnData[t].forGoodsId == e.shopId) {
					e.isHave = true;
					if (DataProl.userCurrentDressUp.decorateId == e.shopId ||
						DataProl.userCurrentDressUp.trailingId == e.shopId)
						e.changeShopAction(1);
					// else
					// 	e.changeShopAction(2);
				}
			})
		}
	}
	/**服务器上的所有商品 */
	public serverAllGoods(allGoods) {
		for (var t = 0; t < allGoods.length; t++) {
			var oneShop: goods = new goods(this);
			oneShop.dataInit(allGoods[t]);
			oneShop.init();
			this.allShopData.push(oneShop);
		}
	}
	/**打开试用 */
	public openTry() {

		// console.log("走一走3");
		var havegoods = [];
		for (var i = 0; i < this.allShopData.length; i++) {
			if (this.allShopData[i].isHave == false) {
				havegoods.push(this.allShopData[i]);
			}
		}
		var index = Math.floor(Math.random() * 4 + 1001);
		// var index = 1001;
		gameShopData.getInstance().trynum = index;
		//console.log("wanjiashiyong"+gameShopData.getInstance().trynum);

		for (var i = 0; i < havegoods.length; i++) {
			// console.log(i);
			// console.log(havegoods[i].isHave);
			// console.log("随机" + index);
			// // console.log("当前装饰" + DataProl.try.decorateId);
			// console.log(havegoods[i].shopId);
			if (havegoods[i].shopId == index) {
				console.log("打开试用");
				// console.log("通过2");
				// console.log(i);
				// // if (this.allShopData[i].shopId != DataProl.try.decorateId) {
				// console.log("走了吗？");
				GameOver.getInstance().isGameover = false;
				havegoods[i].TryButton.visible = true;
				havegoods[i].disGoodsPrice.visible = false;
				havegoods[i].disGoodsGold.visible = false;
				havegoods[i].disGoodsButton.visible = false;
				egret.Tween.get(havegoods[i].TryButton, { loop: true })
					.to({ scaleX: 0.7, scaleY: 0.7 }, 600)
					.to({ scaleX: 1, scaleY: 1 }, 600);
				gameShopData.getInstance().isHaveTry = true;
				console.log(gameShopData.getInstance().isHaveTry);
				return;
			}

		}

	}


	// }
	// }

	/**打开试用中 */
	public openTrying() {
		//console.log("走一走2");
		if (wxtool.Instance.openTrying == true) {

			for (var i = 0; i < this.allShopData.length; i++) {
				console.log("商品名" + this.allShopData[i]);
				console.log("i值" + i);
				if (this.allShopData[i].shopId == undefined) {
					break;
				}
				else if (wxtool.Instance.shopnum == this.allShopData[i].shopId) {
					this.allShopData[i].TryingButton.visible = true;
					this.allShopData[i].TryButton.visible = false;
					wxtool.Instance.openTrying = false;
					break;
				}

			}
		}
	}
	/**关闭试用中 */
	public hideTrying() {
		//console.log("走一走")
		for (var i = 0; i < this.allShopData.length; i++) {
			console.log("商品名" + this.allShopData[i]);
			console.log("i值" + i);
			if (this.allShopData[i].shopId == null || this.allShopData[i].shopId == undefined) {
				break;
			}
			else if (wxtool.Instance.shopnum == this.allShopData[i].shopId) {
				console.log("关闭试用中");
				this.allShopData[i].TryingButton.visible = false;
				wxtool.Instance.hideTrying = false;
				this.allShopData[i].disGoodsPrice.visible = true;
				this.allShopData[i].disGoodsGold.visible = true;
				this.allShopData[i].disGoodsButton.visible = true;
				this.allShopData[i].disGoodsButton.touchEnabled = true;

				DataProl.userCurrentDressUp.decorateId = DataProl.try.decorateId;
				gameShopData.getInstance().isHaveTry = false;
				break;

			}
		}
	}
}
